package com.zf.zffencheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zf.zffencheng.dao.TenantPalaceDao;
import com.zf.zffencheng.model.dto.TenantPalaceFilter;
import com.zf.zffencheng.model.pojo.TenantPalace;
import com.zf.zffencheng.model.vo.TenantPalaceVo;
import com.zf.zffencheng.service.TenantPalaceService;
import com.zf.zffencheng.util.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: TenantPalaceServiceImpl <br/>
 * Description:  <br/>
 * date: 2021/5/28 16:35<br/>
 *
 * @author william <br/>
 */
@Service
public class TenantPalaceServiceImpl extends ServiceImpl<TenantPalaceDao, TenantPalace> implements TenantPalaceService {
    @Override
    public List<TenantPalace> selectByTenantIdDesc(Integer tenantId) {
        QueryWrapper<TenantPalace> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenantId" ,tenantId);
        queryWrapper.orderByDesc("createTime");
        return list(queryWrapper);
    }

    /**
     * 查询大殿id
     *
     * @param filter
     * @return
     */
    @Override
    public Page<TenantPalaceVo> queryFilter(TenantPalaceFilter filter) {
        Page<TenantPalace> offset = new Page<>(filter.getPage(), filter.getPageSize());
        QueryWrapper<TenantPalace> queryWrapper = new QueryWrapper();
        if(null != filter){
            if(StringUtils.hasLength(filter.getPalaceName())){
                queryWrapper.like("PalaceName", filter.getPalaceName());
            }
            if(filter.getTenantId() != null){
                queryWrapper.like("TenantId", filter.getTenantId());
            }
        }
        queryWrapper.orderByDesc("CreateTime");
        Page<TenantPalace> page = page(offset, queryWrapper);
        List<TenantPalaceVo> collect = page.getRecords().stream().map(val -> BeanUtils.copyAs(val, TenantPalaceVo.class))
                .collect(Collectors.toList());
        Page<TenantPalaceVo> tenantPalaceVoPage = new Page<>();
        return tenantPalaceVoPage.setRecords(collect).setTotal(page.getTotal());
    }
}
